package com.jzy.bxb.app.timer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.crypto.CipherInputStream;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jzy.bxb.app.manager.WeatherManager;
import com.jzy.bxb.app.server.App;
import com.jzy.bxb.core.util.HttpUtils;
import com.jzy.bxb.core.util.StringUtil;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.mongo.dao.ConfigCityDao;

/**
 * 天气定时任务
 * 
 * @author JiangZhiYong QQ:359135103
 */
public class WeatherTimer implements Runnable {
	private static final Logger LOGGER=LoggerFactory.getLogger(WeatherTimer.class);

	@Override
	public void run() {
		List<String> cityNames = ConfigCityDao.getCityNames();
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + AppConstant.ALI_WEATHER_APPCODE);
		cityNames.forEach(city -> {
			Map<String, String> querys = new HashMap<String, String>();
			querys.put("city", city);
			try {
				HttpResponse response = HttpUtils.doGet(AppConstant.ALI_WEATHER_HOST, AppConstant.ALI_WEATHER_PATH, "get", headers, querys);
				String info=EntityUtils.toString(response.getEntity());
				LOGGER.info("天气:{}",info);
				if(!StringUtil.isEmpty(info)){
					WeatherManager.getInstance().getWeatherMap().put(city, info);
				}
			} catch (Exception e) {
				LOGGER.error("WeatherTimer", e);
			}
		});

	}

}
